package webServiceClient.academico;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class FrmFecharAtv extends Activity {
	private Button btnClose, btnConfirmar, btnLoadTurma, btnLoadAtc;
	private EditText edtDtaFim, edtHraFim, edtAnoTurma;
	private Spinner cbDisciplina, cbTurma, cbAtividade;
	private CheckBox cbFimAtv;

	private void carregadCombo() {
		String[] codDisciplina;
		try {
			WebServiceClient ws = new WebServiceClient();
			codDisciplina = ws.getDisciplinaProfessor("getDisciplinaProfessor",
					Definicoes.getRaAdm(), Definicoes.getSenhaAdm(),
					Definicoes.getRaAdm());
			ArrayAdapter<String> adp = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, codDisciplina);
			adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			cbDisciplina.setAdapter(adp);
		} catch (Exception e) {
			Toast t = Toast.makeText(this, "Erro na lista - " + e.getMessage(),
					Toast.LENGTH_LONG);
			t.show();
			return;
		}
	}

	private void carregaAtv() {
		String[] codAtividade;
		try {
			WebServiceClient ws = new WebServiceClient();
			codAtividade = ws.getAtividadeDisponivel("getAtividadeDisponivel",
					Definicoes.getRaAdm(), Definicoes.getSenhaAdm(), Definicoes
							.getSessao(cbDisciplina.getSelectedItem()
									.toString(), "|", 1), Definicoes.getSessao(
							cbTurma.getSelectedItem().toString(), "|", 1),
					Integer.parseInt(Definicoes.getSessao(cbTurma
							.getSelectedItem().toString(), "|", 2)));
			ArrayAdapter<String> adp = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, codAtividade);
			adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			cbAtividade.setAdapter(adp);
			adp.notifyDataSetChanged();
		} catch (Exception e) {
			Toast t = Toast.makeText(this,
					"Erro ao preencher listas - " + e.getMessage(),
					Toast.LENGTH_LONG);
			t.show();
			return;
		}
	}

	private void carregaTurma() {
		String[] codTurma;
		try {
			WebServiceClient ws = new WebServiceClient();
			codTurma = ws.getTurmasProfessorAno("getTurmasProfessorAno",
					Definicoes.getRaAdm(), Definicoes.getSenhaAdm(),
					Definicoes.getRaAdm(),
					Integer.parseInt(edtAnoTurma.getText().toString()));
			ArrayAdapter<String> adp = new ArrayAdapter<String>(this,
					android.R.layout.simple_spinner_item, codTurma);
			adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			cbTurma.setAdapter(adp);
			adp.notifyDataSetChanged();
		} catch (Exception e) {
			Toast t = Toast.makeText(this,
					"Erro ao preencher listas - " + e.getMessage(),
					Toast.LENGTH_LONG);
			t.show();
			return;
		}
	}

	private void carregBtn() {
		btnLoadAtc = (Button) findViewById(R.FrmFecharAtv.btnLoadAtv);
		btnClose = (Button) findViewById(R.FrmFecharAtv.btnClose);
		btnConfirmar = (Button) findViewById(R.FrmFecharAtv.btnConfirmar);
		btnLoadTurma = (Button) findViewById(R.FrmFecharAtv.btnLoadTurma);
		edtDtaFim = (EditText) findViewById(R.FrmFecharAtv.edtDataFim);
		edtHraFim = (EditText) findViewById(R.FrmFecharAtv.edtHoraFim);
		edtAnoTurma = (EditText) findViewById(R.FrmFecharAtv.edtAnoTurma);
		cbDisciplina = (Spinner) findViewById(R.FrmFecharAtv.cbDisciplina);
		cbTurma = (Spinner) findViewById(R.FrmFecharAtv.cbTurma);
		cbAtividade = (Spinner) findViewById(R.FrmFecharAtv.cbAtividade);
		cbFimAtv = (CheckBox) findViewById(R.FrmFecharAtv.cbCloseAtv);

		btnConfirmar.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				try {
					int fim = 1;
					if (cbFimAtv.isChecked() == true)
						fim = 0;
					WebServiceClient ws = new WebServiceClient();
					String s = ws.setOffAtividade(
							"setOffAtividade", 
							Definicoes.getRaAdm(), 
							Definicoes.getSenhaAdm(), 
							Definicoes.getSessao(cbAtividade.getSelectedItem().toString(),"|", 1), 
							Definicoes.getSessao(cbDisciplina.getSelectedItem().toString(), "|", 1), 
							Definicoes.getSessao(cbTurma.getSelectedItem().toString(),"|", 1), 
							Integer.parseInt(Definicoes.getSessao(cbTurma.getSelectedItem().toString(),"|", 2)),
							edtDtaFim.getText().toString()+ " " + edtHraFim.getText().toString() + ":00", 
							fim);
					if (s.equals("true")) {
						Toast t = Toast.makeText(FrmFecharAtv.this,
								"Atividade alterada", Toast.LENGTH_LONG);
						t.show();
					} else {
						Toast t = Toast.makeText(FrmFecharAtv.this,
								"Erro ao atualizar atividade",
								Toast.LENGTH_LONG);
						t.show();
					}
				} catch (Exception e) {
					Toast t = Toast.makeText(FrmFecharAtv.this,
							Definicoes._erroServer, Toast.LENGTH_LONG);
					t.show();
				}
			}
		});
		btnLoadAtc.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				carregaAtv();
			}
		});
		btnLoadTurma.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				carregaTurma();
			}
		});
		btnClose.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.frm_fechar_atv);
		carregBtn();
		carregadCombo();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.frm_fechar_atv, menu);
		return true;
	}

}
